<?php $this->headScript()
->appendFile($this->js('plugins/jquery.timers.js'))
->appendFile($this->js('plugins/jquery.blockUI.js'))
->capturestart()?>

jQuery.fn.time_from_seconds = function() {
    return this.each(function() {
        var t = parseInt($(this).text(), 10);
        $(this).data('original', t);
        var h = Math.floor(t / 3600);
        t %= 3600;
        var m = Math.floor(t / 60);
        var s = Math.floor(t % 60);
        $(this).text((h > 0 ? h + ' hour' + ((h > 1) ? 's ' : ' ') : '') +
                     (m > 0 ? m + ' minute' + ((m > 1) ? 's ' : ' ') : '') +
                     s + ' second' + ((s > 1) ? 's' : ''));
    });
};


var tables = <?php echo $this->tableArr?>;
var sel_table =0;
var countId = 0;
// 查詢資料庫更新筆數
function getTableStatus(i) {
  var table = tables[sel_table];
  if (table)
  $("#table-rows").html( i).time_from_seconds();
  else
  $("#table-rows").stopTime();
}

function updbaction(){
	$.getJSON('<?php echo  $this->route('up')?>/table/'+tables[sel_table],function(res){
	   countId ++;
	   if (res.result == 'isUpgrde') {
		  sel_table ++;
		  var detail_str = '';
		} else {
            var detail_str = '--('+res.result+')--';
		}
		if (tables[sel_table]) {
				$("#current-table-name").html(tables[sel_table]);
				updbaction();
		}
		else {
    		$("#upgradeArea").removeClass('loading');
    		$("#upgradeDb").attr('disabled','');
		}
		$("#c-number").html(sel_table+ '/' +tables.length);
		$("#response").prepend(countId+'. '+res.id+'--'+res.item+detail_str+'--- <?php echo $this->translate('upgrade completed')?><br />');
	});

};

$(document).ready(function(){

	$("#upgradeDb").click(function(){

	//   $(document).block('<h1><?php echo $this->translate('uploading ..')?></h1>', { border: '3px solid #a00' });

		sel_table =0;
		$("#complted-number").show();
		$("#current-table").show();
		$(this).attr('disabled','disabled');
		$("#response").empty();
		$("#upgradeArea").addClass('loading');
		updbaction();
		//顯示筆數
        $("#table-rows").everyTime(1000,getTableStatus);
        });

	$("#ressetdb").click(function(){
		window.location.href='<?php echo  $this->route('testdb')?>/resetdb/1';
	})
});
<?php $this->headScript()->captureEnd()?>
<style>
.loading {cursor:wait;}
#complted-number{display:none;margin:10px;background: #ff0;padding:5px}
#c-number {font-weight: bold; color:red}
</style>
<div id="upgradeArea" >
<p><span class="ui-state-default ui-corner-all" style="padding:3px;cursor: pointer;float:right" id="ressetdb">重設SFS3資料庫</span></p>
<div id="upgradeList" style="float:left">
<h1><?php echo $this->translate('database upgrading')?></h1>
<button type="button" name="upgradeBtn" id="upgradeDb"><?php echo $this->translate('upgrade')?></button>
<span id="current-table" style="margin-left:10px;display:none;"><?php echo $this->translate('current upgrade table')?> : <span id="current-table-name"></span></span>
<span id="complted-number"><?php echo $this->translate('completed number')?>: <span id="c-number"></span></span>
<div>花費時間 : <span id="table-rows"></span></div>
<div id="response" style="overflow: auto;height:250px;padding:5px;width:500px"></div>
</div>
<div style="clear:both"></div>
</div>